Hello,
I have some Windows Server 2008 systems that I'm trying to run a powershell script on to delete some temp files, but I keep getting prompted with "Confirm... Y [Yes] [A] Yes to All...... ect" Is there a way to bypass the Confirm?
Thanks,
Tom
Technology Tips and News
Hello,
I have some Windows Server 2008 systems that I'm trying to run a powershell script on to delete some temp files, but I keep getting prompted with "Confirm... Y [Yes] [A] Yes to All...... ect" Is there a way to bypass the Confirm?
Thanks,
Tom
If you are using Remove-Item cmdlet use confirm switch like in below example
Remove-Item .\v.txt -Confirm:$false
Hi Krystian,
I'm using the below command typed directly into PowerShell just for testing purposes. So, I should just be able to add -Confirm:$false to the end and it should not prompt me to Confirm?
get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue
Thanks,
Tom
Yes, if you set $confirm to false explicitly, it shouldn't prompt you to confirm.
get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false
Alternatively, you can try -force parameter as well.
get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Force
PS: Test above changes before trying out in production environment.
I just added -Confirm:$false to the end, but when I run the command locally on the server I'm still getting the following:
Confirm
The item at
Microsoft.Powershell.Core\FileSystem::C:\inetpub\logs\LogFiles\W3SVC1 has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is “Y”):
I then tried it with –Force, but I received the same Confirm prompt.
Yes, it should remove anything older than 14 days.... files or folders / folders and files.
Thanks,
Tom
Do you mind posting your code here for better clarity? I generally delete all temp files in c:\windows\temp with below code. I face no Confirm prompts.
$TempFiles = Get-ChildItem c:\windows\temp -include *.tmp -recurse
$TempFiles | foreach ($_) { remove-item $_.Fullname -force}
Yes, it should remove anything older than 14 days.... files or folders / folders and files.
Thanks,
Here’s the code we are using to delete anything older than 14 days. Any suggestions is much appreciated. Thanks
get-childitem C:\inetpub\logs\LogFiles -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false
This has probably already been answered but I was running into the same problem as you and couldn't find an answer.
Maybe if I post it here I will be able to find it the next time I run into it. :-)
I think this will work for you
get-childitem C:\inetpub\logs\LogFiles -include *.log -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | remove-item -ErrorAction SilentlyContinue -Confirm:$false
replace *.log without ever wildcard you need.
I was executing this
get-childitem -Recurse c:\logs\ | where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif
and getting the same as you
Confirm
The item at
Microsoft.Powershell.Core\FileSystem::C:\logs\blah has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
I replaced it with
get-childitem -Recurse c:\logs\ -include *log*| where {$_.LastWriteTime -le $now.AddDays(-30)} | del -whatif
and like magic no more prompts.
Hello Folks,
I'm having this issue with the "Confirm" dialog continuously popping up, no matter what combination of parameters I try. I've been reading this blog and tried using multiple combinations, to no avail.
Here is my command: Get-ChildItem 'c:\builds\testing' -recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(0)} | del
-ErrorAction SilentlyContinue -Confirm:$false
If any one can help it would be greatly appreciated.
NOTE: I also set the $ConfirmPreference = "None"
Thanks In Advance!
Hello Folks,
I was able to solve this probably, with some internal help.
Here is my command I used to suppress the confirm dialog that I was getting:
Get-ChildItem c:\builds\testing -recurse | where-object {$_.LastWriteTime -le (Get-Date).AddDays(0)} | remove-item -force
-recurse
I hope this will be of some help to someone else that is having the same problem.
Thanks,
Hello,
Does anybody know how can I add exclusions by folder(and all its contents) to this script ?
I have 3-4 folders and their content which I would like to keep!
thanks!
Hi,
You can use the existing where clause to exclude the folders. If you still have questions, you're probably better off starting a new thread, as this one is old and already marked as answered.
Have been there have strugled with that ... but -comfirm:0 has worked for me . also using -verbose but that should be a different matter. you could try $false= 0 , in Your site constant and variable section...
Have been there have strugled with that ... but -comfirm:0 has worked for me . also using -verbose but that should be a different matter. you could try $false= 0 , in Your site constant and variable section...